package com.stopboot.code.generator.utils;

import java.io.File;
import java.util.Arrays;

/**
 * @version: 0.0.1
 * @author: Lianyutao
 * @create: 2020-05-01 19:17
 * @description:
 **/

public class StringUtil {


    private static final char UNDER_LINE = '_';
    private static final char HYPHEN_LINE = '-';

    private static final char OBLIQUE_LINE = '/';

    private static final String[] JAVA_WORLDS = {
            "abstract",
            "assert",
            "boolean",
            "break",
            "byte",
            "case",
            "catch",
            "char",
            "class",
            "const",
            "continue",
            "default",
            "do",
            "double",
            "else",
            "enum",
            "extends",
            "final",
            "finally",
            "float",
            "for",
            "goto",
            "if",
            "implements",
            "import",
            "instanceof",
            "int",
            "interface",
            "long",
            "native",
            "new",
            "package",
            "private",
            "protected",
            "public",
            "return",
            "strictfp",
            "short",
            "static",
            "super",
            "switch",
            "synchronized",
            "this",
            "throw",
            "throws",
            "transient",
            "try",
            "void",
            "volatile",
            "while",
    };


    /**
     * 是否是java关键字
     *
     * @param str
     * @return
     */
    public static boolean isJavaWorld(String str) {
        return Arrays.asList(JAVA_WORLDS).contains(str);
    }


    public static String firstWordToUpper(String str) {
        if (str == null || str == "" || str.length() == 0) {
            return "";
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    public static String pathToPackage(String path) {
        if (path.startsWith("/")) {
            path = path.substring(1, path.length());
        }
        String[] pathArray = path.split("/");
        String newPath = "";
        for (String p : pathArray) {
            String finalP = p;
            if (Arrays.stream(JAVA_WORLDS).anyMatch(e -> e.equals(finalP))) {
                p = p + "x";
            }
            newPath += p + ".";
        }
        return newPath.endsWith(".") ? newPath.substring(0, newPath.length() - 1) : newPath;
    }

    public static String packageToPath(String packageStr) {
        if (packageStr == null || packageStr == "") {
            return "/";
        }
        return packageStr.replaceAll("\\.", "\\" + File.separator);
    }

    /**
     * 首字母转大写
     *
     * @param s
     * @return
     */
    public static String toUpperCaseFirstOne(String s) {
        if (Character.isUpperCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

    /**
     * 首字母转小写
     *
     * @param s
     * @return
     */
    public static String toLowerCaseFirstOne(String s) {
        if (Character.isLowerCase(s.charAt(0))) {
            return s;
        } else {
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }


    /**
     * 横线 转 驼峰
     *
     * @param param
     * @return
     */
    public static String hyphenToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = Character.toLowerCase(param.charAt(i));
            if (c == HYPHEN_LINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 下划线 转 驼峰
     *
     * @param param
     * @return
     */
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = Character.toLowerCase(param.charAt(i));
            if (c == UNDER_LINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }


    /**
     * 斜线 转 驼峰
     *
     * @param param
     * @return
     */
    public static String obliquelineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = Character.toLowerCase(param.charAt(i));
            if (c == OBLIQUE_LINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 表明转Domain
     *
     * @param tableName
     * @return
     */
    public static String tableNameToDomain(String tableName) {
        if (tableName == null || tableName.length() == 0) {
            return "";
        }
        String toCamel = underlineToCamel(tableName);
        return firstWordToUpper(toCamel);
    }


    /**
     * 方法名转为path
     * 方法一般是驼峰命名
     *
     * @param methodName
     * @return
     */
    public static String methodNameToPath(String methodName) {
        if (methodName == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        // 前置字符是否大写
        boolean preCharIsUpperCase = true;
        // 当前字符是否大写
        boolean curreCharIsUpperCase = true;
        // 下一字符是否大写
        boolean nexteCharIsUpperCase = true;
        for (int i = 0; i < methodName.length(); i++) {
            char c = methodName.charAt(i);
            if (i > 0) {
                preCharIsUpperCase = Character.isUpperCase(methodName.charAt(i - 1));
            } else {
                preCharIsUpperCase = false;
            }

            curreCharIsUpperCase = Character.isUpperCase(c);

            if (i < (methodName.length() - 1)) {
                nexteCharIsUpperCase = Character.isUpperCase(methodName.charAt(i + 1));
            }

            if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
                sb.append("/");
            } else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
                sb.append("/");
            }
            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(methodNameToPath("deleteLogicByParams"));
        System.out.println(pathToPackage("/name/aaa/cc/java/do"));
        System.out.println(pathToPackage("name1/aaa/cc/public/do"));
    }


}
